package 希尔排序;

import java.util.Arrays;

public class shellSort {

    public static void shell(int[] array,int gap){
        for(int i=gap;i<array.length;i++){
            int tmp=array[i];
            int j=i-gap;
            for(;j>=0;j-=gap){
                if(array[j]>tmp){
                    array[j+gap]=array[j];
                }else{
                    break;
                }
            }
            array[j+gap]=tmp;
        }
    }

    public static void shellsort(int[] array){
        int gap=array.length;
        while(gap > 1){
            shell(array,gap);
            gap=gap/2;
        }
        shell(array,1);//保证最后是一组
    }

    public static void main(String[] args) {
        int[] array={12,5,18,10,4,2};
        shellsort(array);
        System.out.println(Arrays.toString(array));
    }
}
